import FullPathData from "../../eventbus/FullPathData.js";
import saveManager from "../save/SaveManager.js";
import SaveManager from "../save/SaveManager.js";
import {mapEventBus} from "../../eventbus/map_signal.js";
import FunctionSaveFactory from "../save/FunctionSaveFactory.js";


class RoundWheelManager {
    static getWindow() {
        return FullPathData.window
    }

    static getRoundSave() {
        if (FullPathData.window) {
            let saveManager = new SaveManager(RoundWheelManager.getWindow())
            return saveManager.load_data('round_wheel')
        }
        console.log('不行')
        return null
    }

    static setRoundSave(round) {
        let roundSave = this.getRoundSave() ?? {}
        roundSave.current = round
        new SaveManager(RoundWheelManager.getWindow()).save_data('round_wheel', roundSave);
    }

    static nextRound() {
        let roundSave = this.getRoundSave()
        let ret = roundSave.current + 1
        if (roundSave) {
            this.setRoundSave(ret)
        }
        return ret
    }

    static triggerNextRound() {
        // 直接发送信号
        mapEventBus.emit('next-round', Math.random()); // 记录回合数
        let round_data = this.getRoundSave()
        let event_table = round_data.register // 查看事件注册表
        let now_round = round_data.current
        let events = event_table[now_round] ?? {handler: []}

        for (let event of events.handler) {
            let func = FunctionSaveFactory.FunctionLoad( event)
            if (func) {
                func();
            }
        }
    }
}

export default RoundWheelManager;